package com.wjx.test;

import com.wjx.entity.User;
import com.wjx.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CRUDTest {

    private SqlSession session;

    @Before
    public void init(){
        //利用MyBatisUtil工具类获取数据库的连接
        session = MyBatisUtil.openSession();
    }

    //查询数据
    @Test
    public void testSelection(){
        int rows = session.selectOne("usermp.findUserCount");
        System.out.println(rows);
    }
    //根据姓名查询 findUserByName
    @Test
    public void testSelection_by_username(){
        User user= session.selectOne("usermp.findUserByName","wjx");
        System.out.println(user);
    }
    //插入数据
    @Test
    public void testInsertUser(){
        User user1 = new User("user8", "123456");
        int rows = session.insert("usermp.insertUser", user1);
        System.out.println(rows);
    }
    //修改数据
    @Test
    public void testUpdateUser(){
        User user = session.selectOne("usermp.findUserByName", "user8");
        user.setPassword("wjx789");
        int rows = session.update("usermp.updateUser", user);
        System.out.println(rows);
    }
    //删除
    @Test
    public void testDeleteUser(){
        int rows = session.delete("usermp.deleteUser", "user8");
        System.out.println(rows);
    }



    @After
    public void destory(){
        //关闭连接
        session.close();
    }
}



